﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Control_DepartmentTreeView : System.Web.UI.UserControl
{

    private Srvtools.WebDataSet WMasterDpt;

    private ShowMode _OnlyDpt = ShowMode.OnlyDpt;
    private ShowMode _DptAndJob = ShowMode.DptAndJob;
    private ShowMode _DptAndEmp = ShowMode.DptAndEmp;
    private ShowMode _ShowDptAndJobAndEmp = ShowMode.ShowDptAndJobAndEmp;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            //Bind();

            //int i = WMasterDpt.GetRecordsCount();
        }
    }

    public void Bind(ShowMode SM)
    {
        InitializeComponent();



        string Event = "";
        object[] ret = null;
        object DeptId = null;
        DataSet ds = null;


        TreeNode tn = new TreeNode();
        tn.Text = "USAS";
        tn.Value = "0";


        switch (SM)
        {
            case ShowMode.OnlyDpt:

                TvDpt.ExpandDepth = 1;
                break;
            case ShowMode.DptAndJob:

                TvDpt.ExpandAll();
                Event = "GetCompanyJobCurrentDptId";
                ret = Srvtools.CliUtils.CallMethod("SDepartment", Event, new object[] { });
                break;
            case ShowMode.DptAndEmp:
                if ((Request["ItemParam"].ToString().Split('?'))[0] == "view")
                {
                    DeptId = Srvtools.CliUtils.CallMethod("SDepartment", "GetDptIdByEmpId", new object[] { Convert.ToInt32(Srvtools.CliUtils.GetValue("_UserCode")[1]) })[1];
                    //DeptId = "20,30";
                }

                TvDpt.ExpandAll();
                Event = "GetChildEmpAll";
                ret = Srvtools.CliUtils.CallMethod("SDepartment", Event, new object[] { DeptId });
                break;
            case ShowMode.ShowDptAndJobAndEmp:
                ret = Srvtools.CliUtils.CallMethod("SDepartment", "GetChildEmpAllForEmp", new object[] { DeptId });
                break;
        }
        ds = (DataSet)(Srvtools.CliUtils.CallMethod("SDepartment", "GetChildDptAll", new object[] { "0" }))[1];


        BindBack(ds.Tables[0], tn, SM, Event, ret);

    }

    #region 外部属性
    private DataRow[] GetChild(DataTable dt, string parentDpt, ShowMode SM)
    {

        return dt.Select("Pid=" + parentDpt);

        //}
    }

    public string TreeValue
    {
        get
        {
            return TvDpt.SelectedNode.Value;
        }
        set
        {
            TvDpt.SelectedNode.Value = value;
        }
    }

    public enum ShowMode : int
    {
        OnlyDpt = 1,
        DptAndJob,
        DptAndEmp,
        ShowDptAndJobAndEmp
    }

    public ShowMode ShowDptAndJobAndEmp
    {
        get
        {
            return _ShowDptAndJobAndEmp;
        }
        set
        {
            _ShowDptAndJobAndEmp = value;
        }
    }


    public ShowMode OnlyDpt
    {
        get
        {
            return _OnlyDpt;
        }
        set
        {
            _OnlyDpt = value;
        }
    }

    public ShowMode DptAndJob
    {
        get
        {
            return _DptAndJob;
        }
        set
        {
            _DptAndJob = value;
        }
    }

    public ShowMode DptAndEmp
    {
        get
        {
            return _DptAndEmp;
        }
        set
        {
            _DptAndEmp = value;
        }
    }
    #endregion

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Control_DepartmentTreeView));
        this.WMasterDpt = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMasterDpt)).BeginInit();
        // 
        // WMasterDpt
        // 
        this.WMasterDpt.Active = true;
        this.WMasterDpt.AlwaysClose = true;
        this.WMasterDpt.DataCompressed = false;
        this.WMasterDpt.DeleteIncomplete = true;
        this.WMasterDpt.Guid = null;
        this.WMasterDpt.LastKeyValues = null;
        this.WMasterDpt.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMasterDpt.PacketRecords = 100;
        this.WMasterDpt.Position = -1;
        this.WMasterDpt.RemoteName = "SDepartment.ViewDepartment";
        this.WMasterDpt.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMasterDpt)).EndInit();

    }


    private void BindBack(DataTable dt, TreeNode tn, ShowMode SM, string Event, object[] ret)
    {
        //非员工邦定
        if (tn.Value == "0")
        {
            tn.ImageUrl = "~/image/MenuTree/tabicons_019.gif";


            switch (SM)
            {
                case ShowMode.OnlyDpt:
                    tn.NavigateUrl = Request.Url.ToString().Substring(0, Request.Url.ToString().IndexOf("aspx")) + "aspx?action=" + tn.Value;
                    break;
                case ShowMode.DptAndJob:
                    break;
                case ShowMode.DptAndEmp:
                    break;

            }
            TvDpt.Nodes.Add(tn);

            TreeNode[] ctnn = Do(SM, tn.Value, ret);

            for (int j = 0; j < ctnn.Length; j++)
            {
                tn.ChildNodes.Add(ctnn[j]);
            }
        }





        DataRow[] dr = GetChild(dt, tn.Value, SM);

        for (int i = 0; i < dr.Length; i++)
        {
            TreeNode[] ctnn = Do(SM, (dr[i].ItemArray)[0].ToString(), ret);

            TreeNode tnn = new TreeNode();
            tnn.Value = (dr[i].ItemArray)[0].ToString();
           
            tnn.ImageUrl = "~/image/MenuTree/tabicons_058.gif";
            
            tnn.Text = (dr[i].ItemArray)[1].ToString();

            switch (SM)
            {
                case ShowMode.OnlyDpt:
                    tnn.NavigateUrl = Request.Url.ToString().Substring(0, Request.Url.ToString().IndexOf("aspx")) + "aspx?action=" + tnn.Value;
                    break;
                case ShowMode.DptAndJob:
                    break;
                case ShowMode.DptAndEmp:
                    if ((dr[i].ItemArray)[4].ToString() != "")
                    {
                        string TnnHr = "";
                        for (int j = 0; j < 30 - (dr[i].ItemArray)[1].ToString().Length; j++)
                        {
                            TnnHr += "—";
                        }


                        tnn.Text = (dr[i].ItemArray)[1].ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;<font style='color:silver'>" + TnnHr + "</font>&nbsp;&nbsp;" + (dr[i].ItemArray)[4].ToString() + "（负责人）";
                    }

                    
                    break;
                case ShowMode.ShowDptAndJobAndEmp:
                    
                    if ((dr[i].ItemArray)[4].ToString() != "")
                    {
                        string TnnHr = "";
                        for (int j = 0; j < 30 - (dr[i].ItemArray)[1].ToString().Length; j++)
                        {
                            TnnHr += "—";
                        }


                        tnn.Text = (dr[i].ItemArray)[1].ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;<font style='color:silver'>" + TnnHr + "</font>&nbsp;&nbsp;" + (dr[i].ItemArray)[4].ToString() + "（负责人）";
                    }
                   
                    break;

            }


            tn.ChildNodes.Add(tnn);

            for (int j = 0; j < ctnn.Length; j++)
            {
                if (tn.Value == "-1")
                {

                    TvDpt.Nodes.Add(ctnn[j]);
                }
                else
                {
                    tn.ChildNodes.Add(ctnn[j]);
                }


            }

            

            BindBack(dt, tnn, SM, Event, ret);
        }
    }


    private TreeNode[] Do(ShowMode SM, string parentId, object[] ret)
    {

        string Img = "";
        string Rq = "";
        switch (SM)
        {
            case ShowMode.OnlyDpt:
                return new TreeNode[0];
            case ShowMode.DptAndJob:

                Img = "<img src='../image/MenuTree/office_679.png' border='0'/>";
                Rq = Request.Url.ToString().Substring(0, Request.Url.ToString().IndexOf("aspx")) + "aspx?";
                break;
            case ShowMode.DptAndEmp:

                if (Request["action"] == null)
                {
                    Rq = "-1";
                    Rq = Request.Url.ToString().Replace("action=" + Rq, "") + "&";
                }
                else
                {
                    Rq = Request["action"].ToString();
                    Rq = Request.Url.ToString().Replace("action=" + Rq, "") + "&";
                }
                break;

        }

        //员工排列显示
        DataRow[] dr = ((DataSet)ret[1]).Tables[0].Select("Pid=" + parentId);


        TreeNode[] tn = new TreeNode[dr.Length];

        for (int i = 0; i < dr.Length; i++)
        {
            TreeNode tnn = new TreeNode();

            tnn.Value = dr[i][0].ToString();
            int TnnLength = 0;
            string TnnHr = "";
            switch (SM)
            {
                case ShowMode.DptAndEmp:
                    if ((bool)dr[i][2])
                    {
                        Img = "<img src='../image/MenuTree/tabicons_137.gif'  border='0'/></span>";

                    }
                    else
                    {

                        Img = "<img src='../image/MenuTree/tabicons_136.gif' border='0'></span>";
                    }
                    TnnLength = dr[i]["JobName"].ToString().Length;
                    
                    for (int j = 0; j < 30 - TnnLength; j++)
                    {
                        TnnHr += "—";
                    }

                    if (Request["ItemParam"].ToString() != "")
                    {
                        string[] iparar = Request["ItemParam"].ToString().Split('?');
                        if (iparar[0] == "edit")
                        {
                            tnn.Text = "&nbsp;&nbsp;<a href='javascript:void(0)' onclick=\"location.href='EmployeeDetail.aspx?eid=" + tnn.Value + "&mode=" + iparar[1] + "&action=edit&do=false';return false\" class='Show'>&nbsp;&nbsp;" + Img + "&nbsp;&nbsp;" + dr[i]["JobName"].ToString() + "&nbsp;&nbsp;" + TnnHr + "&nbsp;&nbsp;" + dr[i][1].ToString() + "</a>";
                        }
                        else if (iparar[0] == "view")
                        {
                            tnn.Text = "&nbsp;&nbsp;<a href='javascript:void(0)' onclick=\"location.href='EmployeeDetail.aspx?eid=" + tnn.Value + "&mode=" + iparar[1] + "&action=view&do=false';return false\" class='Show'>&nbsp;&nbsp;" + Img + "&nbsp;&nbsp;" + dr[i]["JobName"].ToString() + "&nbsp;&nbsp;" + TnnHr + "&nbsp;&nbsp;" + dr[i][1].ToString() + "</a>";
                        }
                    }
                    break;
                case ShowMode.ShowDptAndJobAndEmp:
                    if ((bool)dr[i][2])
                    {
                        Img = "<img src='../image/MenuTree/tabicons_137.gif'  border='0'/></span>";

                    }
                    else
                    {

                        Img = "<img src='../image/MenuTree/tabicons_136.gif' border='0'></span>";
                    }
                    TnnLength = dr[i]["JobName"].ToString().Length;

                    for (int j = 0; j < 30 - TnnLength; j++)
                    {
                        TnnHr += "—";
                    }

                    tnn.Text = "&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' class='Show'>&nbsp;&nbsp;" + Img + "&nbsp;&nbsp;" + dr[i]["JobName"].ToString() + "&nbsp;&nbsp;" + TnnHr + "&nbsp;&nbsp;" + dr[i][1].ToString() + "</a>";
                    // tnn.Text = "&nbsp;&nbsp;" + Img + dr[i]["JobName"].ToString() + "——————————————;" + dr[i][1].ToString();
                    break;
                default:
                    tnn.Text = "&nbsp;&nbsp;<a href='" + Rq + "action=" + tnn.Value + "' class='Show'/>"
                     + Img + "&nbsp;&nbsp;" + dr[i][1].ToString() + "</a>";
                    break;

            }


            //tnn.NavigateUrl = Request.Url.ToString().Substring(0, Request.Url.ToString().IndexOf("aspx")) + "aspx?action=" + tnn.Value;

            tn[i] = tnn;

            //123
        }
        return tn;

    }
}
